package com.jeecms.wicool.dao.shops.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.jeecms.cms.entity.main.CmsUser;
import com.jeecms.common.hibernate3.Finder;
import com.jeecms.common.hibernate3.HibernateBaseDao;
import com.jeecms.common.page.Pagination;
import com.jeecms.wicool.dao.shops.ServiceUserDao;
import com.jeecms.wicool.entity.shops.ServiceUser;

@Repository
public class ServiceUserDaoImpl extends HibernateBaseDao<ServiceUser, Integer> implements
ServiceUserDao {

	@Override
	protected Class<ServiceUser> getEntityClass() {
		// TODO Auto-generated method stub
		return ServiceUser.class;
	}

	@Override
	public Pagination getPage(String serviceId,String serviceName, int pageNo, int pageSize, CmsUser user) {
		Finder f = Finder.create("select bean from ServiceUser bean where 1=1 ");
		if (!StringUtils.isBlank(serviceId)) {
			f.append(" and bean.serviceId = :serviceId");
			f.setParam("serviceId", serviceId);
		}
		if (!StringUtils.isBlank(serviceName)) {
			f.append(" and bean.serviceName = :serviceName");
			f.setParam("serviceName", serviceName);
		}if(user.getShopId()!=null){
			f.append(" and bean.shop.id = :shopId");
			f.setParam("shopId",user.getShopId());
		}
		f.append(" order by bean.serviceId desc");
		return find(f, pageNo, pageSize);
	}

	@Override
	public ServiceUser findById(Integer id) {
		ServiceUser entity = get(id);
		return entity;
	}

	@Override
	public ServiceUser save(ServiceUser bean) {
		getSession().save(bean);
		return bean;
	}

	@Override
	public ServiceUser deleteById(Integer id) {
		ServiceUser entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
		}
		return entity;
	}

	@Override
	public List<ServiceUser> getAll(CmsUser user) {
		Finder f = Finder
				.create("select bean from ServiceUser bean where 1 = 1 ");
		if(user.getShopId()!=null){
			f.append(" and bean.shop.id = :shopId");
			f.setParam("shopId",user.getShopId());
		}
		f.append(" order by bean.serviceId desc");
		return find(f);
	}

}